********************************************************************************
*Individual coholding
gen cohold_day = 0 
replace cohold_day = 1 if cons1 >=3 & od_exp >=3
bysort ind_id: egen coholder = max(cohold_day)

********************************************************************************
*Household coholding (note: values for singles are and must be missing)
gen cohold_day_HH = 0 if od_exp_HH !=. & cons1_HH !=. & alwaysin2memberhh ==1
replace cohold_day_HH = 1 if ///
	od_exp_HH>=3 & od_exp_HH !=. & ///
	cons1_HH>=3 & cons1_HH !=. & ///
	alwaysin2memberhh==1
	
bysort ind_id: egen coholder_HH = max(cohold_day_HH) if ///
	od_exp_HH !=. & cons1_HH!=. & ///
	alwaysin2memberhh ==1
********************************************************************************

*Unnecessary overdraft in nominal terms
gen unnec_od = cash if od_amount>=cash
replace unnec_od = od_amount if cash>=od_amount

gen unnec_od_HH = cash_HH if od_amount_HH >= cash_HH & od_amount_HH != . & cash_HH !=.
replace unnec_od_HH = od_amount_HH if cash_HH >= od_amount_HH & od_amount_HH != . & cash_HH!=.

*Unit-specific unnecessary overdraft (nominal terms)
bysort ind_id: egen unnec_od_mean = mean(unnec_od)
bysort ind_id: egen unnec_od_HH_mean = mean(unnec_od_HH)

*Unnecessary overdraft divided by individual-specific average daily expenditures
gen unnec_od_exp = cons1 if od_exp>=cons1
replace unnec_od_exp = od_exp if cons1>=od_exp

gen unnec_od_exp_HH = cons1_HH if od_exp_HH>=cons1_HH & od_exp_HH != . & cons1_HH != . 
replace unnec_od_exp_HH = od_exp_HH if cons1_HH>=od_exp_HH & od_exp_HH != . & cons1_HH != .

*Unit-specific average unnecessary overdraft (nominal terms)
bysort ind_id: egen unnec_od_exp_mean = mean(unnec_od_exp)
bysort ind_id: egen unnec_od_exp_HH_mean = mean(unnec_od_exp_HH)

*Unnecessary overdraft divided by individual-specific monthly income
gen unnec_od_ISminc = unnec_od / month_total_income_mean
gen unnec_od_ISminc_HH = unnec_od_HH / month_total_income_HH_mean

foreach var in ///
unnec_od {
gen `var'_rel = `var' / `var'_mean
}
***************************************************************************
***Cost of coholding

*Interest cost for given day
gen int_cost = unnec_od*0.13/365 
gen int_cost_HH = unnec_od_HH*0.13/365

*Annualized daily costs
gen annualized_int_cost = int_cost*365
gen annualized_int_cost_HH = int_cost_HH*365

*For each individual: Total interest costs of coholding.
bysort ind_id: egen tot_int_cost = sum(int_cost) if int_cost !=.
bysort ind_id: egen tot_int_cost_HH = sum(int_cost_HH) if int_cost_HH !=.

*For each individual: Total annualized interest cost
gen int_cost_annu = tot_int_cost/884*365
gen int_cost_annu_HH = tot_int_cost_HH/884*365

********************************************************************************
***COHOLD COST TYPE
summarize tot_int_cost if coholder==1, detail
local median = r(p50)
gen costly_coholder = 0 if tot_int_cost < `median' & coholder==1
replace costly_coholder = 1 if tot_int_cost >= `median' & coholder==1
//costly coholder has total interest costs of coholding above median
********************************************************************************
***COHOLD INTENSITY
*Create cohold day degree variable
summ unnec_od_exp if cohold_day==1, detail
local median = r(p50)
gen CoholdDayDegree = 1 if cohold_day==0
replace CoholdDayDegree = 2 if cohold_day==1 & unnec_od_exp < `median'
replace CoholdDayDegree = 3 if cohold_day==1 & unnec_od_exp >=`median'

bysort ind_id: egen max_CoholdDayDegree = max(CoholdDayDegree)

********************************************************************************
***COHOLD INTENSITY TYPE
gen intense_coholder = 1 if max_CoholdDayDegree == 3 & coholder==1
replace intense_coholder = 0 if max_CoholdDayDegree != 3 & coholder==1
//intense coholder whose maximum relative unnecessary overdraft was above median

********************************************************************************
***COHOLD FREQUENCY TYPE
bysort ind_id: egen no_days_cohold = total(cohold_day)
gen cohold_days_share = no_days_cohold/884 //884 is the total number of observation days for an individual

gen cohold_type = 1 if coholder==0
replace cohold_type = 2 if coholder==1 & cohold_days_share < 0.5
replace cohold_type = 3 if coholder==1 & cohold_days_share >=0.5

gen frequent_coholder = 0 if cohold_type == 2
replace frequent_coholder = 1 if cohold_type == 3

********************************************************************************
***Create sequences for each coholding spell

tsset new_ind_id edate
tsspell cohold_day, end(_end) seq(_seq) spell(_spell)

tsset new_ind_id edate
tsspell cohold_day_HH, end(_end_HH) seq(_seq_HH) spell(_spell_HH)

********************************************************************************
***NUMBER OF COHOLDING SPELLS
bysort ind_id: egen nr_spells = total(dummy) if _end == 1 & cohold_day == 1 
bysort ind_id: egen max_nr_spells = max(nr_spells) 
replace nr_spells = max_nr_spells
drop max_nr_spells

bysort ind_id: egen nr_spells_HH = total(dummy) if _end_HH == 1 & cohold_day_HH == 1 
bysort ind_id: egen max_nr_spells_HH = max(nr_spells_HH) if cohold_day_HH != . 
replace nr_spells_HH = max_nr_spells_HH
drop max_nr_spells_HH

********************************************************************************
*Cohold length (or cohold duration)

*Create a single ID for each individual-specific coholding spell
sort new_ind_id edate
egen ind_spell_id = group(new_ind_id _spell)
sort new_ind_id edate
egen ind_spell_id_HH = group(new_ind_id _spell_HH)

*Generate sequence within each spell
bysort ind_spell_id: egen ind_spell_obs = seq()
bysort ind_spell_id_HH: egen ind_spell_obs_HH = seq() 

*Calculate length of each coholding spell
bysort ind_spell_id: egen cohold_length = sum(dummy) if cohold_day==1
bysort ind_spell_id_HH: egen cohold_length_HH = sum(dummy) if cohold_day_HH==1

*Mean duration of coholding, at individual and household level
bysort ind_spell_id: egen cohold_length_obs = seq() if cohold_day==1
bysort ind_spell_id_HH: egen cohold_length_HH_obs = seq() if cohold_day_HH==1
bysort ind_id: egen indmean_cohold_length = mean(cohold_length) if cohold_length_obs ==1 //longer coholding spells have less weight in this mean
bysort ind_id: egen max_indmean_cohold_length = max(indmean_cohold_length)
replace indmean_cohold_length = max_indmean_cohold_length
bysort ind_id: egen HHmean_cohold_length = mean(cohold_length_HH) if cohold_length_HH_obs ==1
bysort ind_id: egen max_HHmean_cohold_length = max(HHmean_cohold_length)
replace HHmean_cohold_length = max_HHmean_cohold_length

********************************************************************************
***Measures by cohold spell

*Unnecessary overdraft in consumption day terms by individual spell
bysort ind_spell_id: egen cohold_amount_exp = mean(unnec_od_exp) if cohold_day==1
bysort ind_spell_id_HH: egen cohold_amount_exp_HH = mean(unnec_od_exp_HH) if cohold_day==1

*Unnecessary overdraft divided by individual-specific monthly income
bysort ind_spell_id: egen cohold_amount_ISminc = mean(unnec_od_ISminc) if cohold_day==1
bysort ind_spell_id_HH: egen cohold_amount_ISminc_HH = mean(unnec_od_ISminc_HH) if cohold_day==1

*Cost of coholding by spell
bysort ind_spell_id: egen cohold_cost_spell = mean(int_cost) if cohold_day==1
bysort ind_spell_id_HH: egen cohold_cost_spell_HH = mean(int_cost_HH) if cohold_day==1

********************************************************************************
*** Creation of cohold_spell_cat variable
*0 = no coholding, 
*1 = coholding just starts, (the day before coholding starts and the day on which coholding is present)
*2 = first 1/3 of coholding spell,
*3 = second and intermediate 1/3 of coholding spell, 
*4 = last 1/3 of coholding spell, 
*5 = coholding just ends (the day before coholding ends and the day on which coholding is no longer present)
 
*Coholding category 1: 2 days of start, intermediate period, 2 days of ending
sort ind_id edate
gen cohold_spell_cat_1 = 1     if ind_id[_n]==ind_id[_n+1] & cohold_day[_n]==0 & cohold_day[_n+1]==1  //first day before coholding
replace cohold_spell_cat_1 = 1 if ind_id[_n]==ind_id[_n-1] & cohold_day[_n]==1 & cohold_day[_n-1]==0 //first day of coholding
replace cohold_spell_cat_1 = 3 if ind_id[_n]==ind_id[_n+1] & cohold_day[_n]==1 & cohold_day[_n+1]==0 //last day of coholding
replace cohold_spell_cat_1 = 3 if ind_id[_n]==ind_id[_n-1] & cohold_day[_n]==0 & cohold_day[_n-1]==1  //first day after coholding
replace cohold_spell_cat_1 = 2 if cohold_day == 1 & cohold_spell_cat_1 != 1 & cohold_spell_cat_1!=3 //in the middle of coholding period
replace cohold_spell_cat_1 = 0 if coholder==1 & cohold_spell_cat_1 == . & cohold_day ==0

*Coholding category 2: First, second and third 1/3 of coholding spell
bysort ind_spell_id: egen max_seq = max(_seq) if cohold_day == 1
gen cohold_spell_cat_2 = 3/max_seq*_seq if cohold_day==1
replace cohold_spell_cat_2 = 1 if cohold_spell_cat_2 >0 & cohold_spell_cat_2<=1
replace cohold_spell_cat_2 = 2 if cohold_spell_cat_2 >1 & cohold_spell_cat_2<=2
replace cohold_spell_cat_2 = 3 if cohold_spell_cat_2 >2 & cohold_spell_cat_2<=3
replace cohold_spell_cat_2 = . if max_seq < 3
replace cohold_spell_cat_2 = 0 if cohold_spell_cat_2==. & cohold_day==0 & coholder==1

*Final cohold category variable
*Mixture between cohold_spell_cat1 and cohol_spell_cat2
*Note: order is important!
gen cohold_spell_cat = 0 if coholder==1 & cohold_day==0 //cohold_spell_cat only defined for coholders
replace cohold_spell_cat = 2 if cohold_spell_cat_2  == 1
replace cohold_spell_cat = 3 if cohold_spell_cat_2 == 2
replace cohold_spell_cat = 4 if cohold_spell_cat_2==3
replace cohold_spell_cat = 1 if cohold_spell_cat_1==1 //leave this order!
replace cohold_spell_cat = 5 if cohold_spell_cat_1 == 3

********************************************************************************
*Generate cohold spell category as unit of analysis
tsset new_ind_id edate
tsspell cohold_spell_cat, seq(_seqcat) end(_endcat) spell(_spellcat)
sort ind_id edate
egen cohold_spell_cat_id = group(ind_id _spellcat cohold_spell_cat)
bysort cohold_spell_cat_id: egen cohold_spell_cat_uoa = seq()
replace cohold_spell_cat_uoa = . if cohold_spell_cat_uoa !=1 //one day within a given coholding spell category is marked as 1 


********************************************************************************
*Coholding starts and coholding ends
sort ind_id edate

*Dummy = 1 if start (end) and 0 on all other days 
gen pr_cohold_start = 1     if ind_id[_n]==ind_id[_n+1] & cohold_day[_n]==0 & cohold_day[_n+1]==1
replace pr_cohold_start = 1 if ind_id[_n-1]==ind_id[_n] & cohold_day[_n-1]==0 & cohold_day[_n]==1
replace pr_cohold_start = 0 if pr_cohold_start != 1 & coholder==1

gen pr_cohold_end = 1 if ind_id[_n]==ind_id[_n+1] & cohold_day[_n]==1 & cohold_day[_n+1]==0
replace pr_cohold_end = 1 if ind_id[_n-1]==ind_id[_n] & cohold_day[_n-1]==1 & cohold_day[_n]==0
replace pr_cohold_end = 0 if pr_cohold_end != 1 & coholder==1

*For each unique individual-month
foreach var in pr_cohold_start pr_cohold_end {
bysort ind_emy: egen `var'_month = mean(`var')
}

*Share of cohold starts (ends) relative to all starts (end), by th day in month
bysort ind_emy_seq: egen agg_pr_cohold_start_DIM = total(pr_cohold_start)
bysort dum: egen agg_pr_cohold_start = total(pr_cohold_start)
gen pr_cohold_start_DIM = agg_pr_cohold_start_DIM / agg_pr_cohold_start

bysort ind_emy_seq: egen agg_pr_cohold_end_DIM = total(pr_cohold_end)
bysort dum: egen agg_pr_cohold_end = total(pr_cohold_end)
gen pr_cohold_end_DIM = agg_pr_cohold_end_DIM / agg_pr_cohold_end

********************************************************************************
*Cohold "runup" (periods right before or right after coholding)
bysort ind_spell_id: egen max_seq_nocohold = max(_seq) if cohold_day==0
gen cohold_runup_cat = 2/max_seq_nocohold*_seq if cohold_day==0
replace cohold_runup_cat = 1 if cohold_runup_cat >0 & cohold_runup_cat<=1
replace cohold_runup_cat = 2 if cohold_runup_cat >1 & cohold_runup_cat<=2
replace cohold_runup_cat = 0 if cohold_day==1

*Cohold days for frequent or intense coholders
gen cohold_day_frequent = cohold_day if frequent_coholder==1
gen cohold_day_intense = cohold_day if intense_coholder==1
gen cohold_day_costly = cohold_day if costly_coholder==1

*Cohold spells for frequent or intense coholders
gen cohold_spell_cat_frequent = cohold_spell_cat if frequent_coholder==1
gen cohold_spell_cat_intense = cohold_spell_cat if intense_coholder==1
gen cohold_spell_cat_costly = cohold_spell_cat if costly_coholder==1

********************************************************************************
*Expenditure supercategories relative to unnecessary overdraft
foreach var in dur dur_ex nondur nondur_ex tempt {
gen `var'_unnec_od  = `var' / unnec_od
}

********************************************************************************
*Key variables by cohold spell category
foreach var in ///
cr_utiliz ///
dur_ex_exp nondur_ex_exp ///
ca_count_rel sa_count_rel ccard_count_rel ///  //nr of accounts relative to mean
dur_unnec_od dur_ex_unnec_od nondur_unnec_od nondur_ex_unnec_od ///
tempt_unnec_od ///
od_exp cons1 unnec_od_exp ///
dur_exp nondur_exp tempt_exp ///
	cash_exp od_amount_exp total_exp_exp nec_exp unnec_exp ///
	groc_exp fuel_exp alc_exp rmf_exp hi_exp hs_exp transp_exp ca_exp pharm_exp ///
	recr_exp lottery_exp_exp gambling_exp_exp gaming_exp charities_exp ///
	special_exp sa_exp ts_exp holiday_exp ///
	total_income_rel reg_income_rel salary_rel irr_income_rel ///
	sa_bal_rel ca_bal_rel ccard_bal_rel sa_lim_rel ca_lim_rel ccard_lim_rel ///
	drattarv_rel fit_rel utvextir_rel total_fin_cost_rel od_amount_rel cash_rel ///
	liquidity_rel unnec_od_rel ///
	new_car pay_day income_day windfall_dum total_logins {
bysort cohold_spell_cat_id: egen `var'_spell = mean(`var') 
}

********************************************************************************
*For high or low amounts of coholding for given coholding spells
summ cohold_amount_exp if cohold_day==1, detail //mean of unnec_od_exp by spell
gen median = r(p50)
gen unnec_od_exp_high = unnec_od_exp_spell if cohold_amount_exp > median
gen unnec_od_exp_low = unnec_od_exp_spell if cohold_amount_exp < median
gen cohold_spell_cat_high = cohold_spell_cat if cohold_amount_exp > median
gen cohold_spell_cat_low = cohold_spell_cat if cohold_amount_exp < median
drop median

*Cohold spells with expecially high vs low amounts of coholding, dummy
gen cohold_spell_high_dum = 0 if cohold_day ==0 & coholder==1
replace cohold_spell_high_dum = 1 if ///
cohold_day ==1 & unnec_od_exp_high > 0  & unnec_od_exp_high!=.

gen cohold_spell_low_dum = 0 if cohold_day ==0 & coholder==1
replace cohold_spell_low_dum = 1 if ///
cohold_day ==1 & unnec_od_exp_low > 0  & unnec_od_exp_low!=.


